<?php
class PlanController extends  IndexController
{
    public function init()
    {
		parent::init();
        $this->utl            = Yaf_Registry::get("utl");
        $this->PlanModel      = new CollPlanModel();
        $this->ArticleType    = new ArticleModel();
        $this->UserLogsModel  = new LogsModel();
        $this->SourceModel    = new SourceModel();
        $this->Session        = Yaf_Registry::get("session");
    }

    public function listAction()
    {
        $source_info       = $this->SourceModel->getSourceInfo();
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $collplan_info     = $this->PlanModel->selectCollPlan($startLimit,$offset);
        $allCounts         = $this->PlanModel->getAllCount();
        $allpage           = $this->utl::page('/Plan/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $collplan_list     = array();
        foreach ($collplan_info AS $k => $v)
        {
            $collplan_list[$k] = $v;
            $collplan_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $collplan_list[$k]['ends_time'] = date('Y-m-d H:i:s', (int) $v['end_time']);
            $collplan_list[$k]['rows'] = $allCounts[0]['rows'];
            $collplan_list[$k]['every_page'] = $offset;
            foreach ($source_info  AS $ks => $vs)
            {
                if ($v['source_id'] === $vs['source_id'])
                {
                    $collplan_list[$k]['source_name'] = $vs['name'];break;
                }
            }
        }
        foreach ($collplan_list as $key => $value) 
        {
            if($value['status'] == 1)
            {
                $collplan_list[$key]['plan_status'] = '是';
            }
            else if($value['status'] == 0){
                $collplan_list[$key]['plan_status'] ='否';
            }
        }
        $this->getView()->assign("collplan_list",$collplan_list);
        $this->getView()->assign("source_list",$source_info);
        $this->getView()->assign("allpage",$allpage);
    }

    public function pageAction()
    {
        $source_info       = $this->SourceModel->getSourceInfo();
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $collplan_info     = $this->PlanModel->selectCollPlan($startLimit,$offset);
        $allCounts         = $this->PlanModel->getAllCount();
        $allpage           = $this->utl::page('/Plan/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $collplan_list     = array();
        foreach ($collplan_info AS $k => $v)
        {
            $collplan_list[$k] = $v;
            $collplan_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $collplan_list[$k]['ends_time'] = date('Y-m-d H:i:s', (int) $v['end_time']);
            $collplan_list[$k]['rows'] = $allCounts[0]['rows'];
            $collplan_list[$k]['every_page'] = $offset;
            foreach ($source_info  AS $ks => $vs)
            {
                if ($v['source_id'] === $vs['source_id'])
                {
                    $collplan_list[$k]['source_name'] = $vs['name'];break;
                }
            }
        }
        foreach ($collplan_list as $key => $value) 
        {
            if($value['status'] == 1)
            {
                $collplan_list[$key]['plan_status'] = '是';
            }
            else if($value['status'] == 0){
                $collplan_list[$key]['plan_status'] ='否';
            }
        }
        $this->getView()->assign("collplan_list",$collplan_list);
        $this->getView()->assign("articletype_list",$articletype_info);
        exit(json_encode($collplan_list, true));
    }

    public function currentPageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        =$offset*($page-1);
        $collplan_info     = $this->PlanModel->selectCollPlan($startLimit,$offset);
        $allCounts         = $this->PlanModel->getAllCount();
        $allpage = $this->utl::page('/Plan/list', $page, $allCounts[0]['rows'], $offset, $status='');
        exit($allpage);
    }

    public function delAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->forward('Plan', 'list');
            return;
        }
        $rs = $this->PlanModel->del($id);
        $session_del = array();
        $user_info = $this->Session->user_info;
        foreach ($user_info as $k => $v) 
        {
            $session_del[$k]['user_id'] = $v['user_id'];
            $session_del[$k]['table_name'] = "plan";
            $session_del[$k]['action'] = "del";
        }
        $this->UserLogsModel->addUser($session_del);
        header('Content-type:application/json;charset:utf8');
        if (false === $rs)
        {
            exit('{"status":false,"info":"删除失败！"}');
            return;
        }
        exit('{"status":true,"info":"删除成功"}');
        return true;
    }

    public function addAction()
    {
        if ($this->getRequest()->isPost())
        {
            //$post为提交过来的数据
            $post = $this->getRequest()->getPost();
            //var_dump($post);
            header('Content-type:application/json;charset:utf8');
            $rs = $this->PlanModel->add($post);
            $session_add = array();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_add[$k]['user_id'] = $v['user_id'];
                $session_add[$k]['table_name'] = "plan";
                $session_add[$k]['action'] = "add";
            }
            $this->UserLogsModel->addUser($session_add);
            if (false === $rs)
            {
                exit('{"status":false,"info":"添加失败！"}');
                return;
            }
            exit('{"status":true,"info":"采集计划添加成功！"}');
        }
    }

    public function editAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->forward('Plan', 'list');
            return;
        }
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->PlanModel->edit($post, $id);
            $session_edit = array();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_edit[$k]['user_id'] = $v['user_id'];
                $session_edit[$k]['table_name'] = "plan";
                $session_edit[$k]['action'] = "edit";
            }
            $this->UserLogsModel->addUser($session_edit);
            if (false === $rs)
            {
                exit('{"status":false,"info":"编辑失败！"}');
                return;
            }
            exit('{"status":true,"info":"编辑成功！"}');
        }
    }
}
?>